//
//  XBridgeWebViewController.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/10/11.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "XBridgeWebViewController.h"
#import "XBridgeWebView.h"

@interface XBridgeWebViewController ()

@property (nonatomic, strong) XBridgeWebView *webView;

@end

@implementation XBridgeWebViewController {
    NSString *_webUrlString;
}

- (instancetype)initWithWebUrlString:(NSString *)url {
    _webUrlString = url;
    self = [super init];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView = [[XBridgeWebView alloc] initWithFrame:CGRectZero configuration:[self createWebConfig]];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    if (_webUrlString) {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_webUrlString]];
        [self.webView loadRequest:request];
    }
}

- (WKWebViewConfiguration *)createWebConfig {
    NSString *js = @"getMessage('iOS')";
    WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addUserScript:script];
    return config;
}



@end
